/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class MainView extends JPanel {
    
    private JPanel panelCalculadora;
    private PanelStandard panelStandard;
    private PanelAvanzado panelAvanzado;
    private Pantalla entrada;
    private JFrame ventana;

    public MainView(JFrame frame) {
        this.ventana = frame;
        setLayout(new BorderLayout());
        setFocusable(false);
        init();
    }
    
    private void init()
    {
        entrada = new Pantalla();
        panelStandard = new PanelStandard(entrada);
        panelAvanzado = new PanelAvanzado(entrada);
        panelCalculadora = new JPanel();
        panelCalculadora.setLayout(new BoxLayout(panelCalculadora, BoxLayout.Y_AXIS));
        add(createInputPanel(), BorderLayout.NORTH);
        panelCalculadora.add(panelStandard);
        JScrollPane scrollPane =  new JScrollPane(panelCalculadora);
        add(scrollPane, BorderLayout.CENTER);
        add(createOptionPanel(), BorderLayout.LINE_START);
    }
    
    private JPanel createInputPanel() {
        JPanel inputPanel = new JPanel();
        inputPanel.setFocusable(false);
        inputPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        entrada.setSize(50, 40);
        inputPanel.add(this.entrada);

        return inputPanel;
    }
    
    private JPanel createOptionPanel()
    {
        JPanel option = UIUtils.createJPanelWithTitle("Opciones");
        CalculatorOptionAction listener = new CalculatorOptionAction();
        option.setFocusable(false);
        option.setLayout(new BoxLayout(option, BoxLayout.PAGE_AXIS));
        
        JRadioButton standard = new JRadioButton("Estandar");
        standard.setSelected(true);
        standard.setFocusable(false);
        standard.addActionListener(listener);
        
        JRadioButton advance = new JRadioButton("Avanzado");
        advance.setFocusable(false);
        advance.addActionListener(listener);
        
        ButtonGroup optionsGroup = new ButtonGroup();
        optionsGroup.add(standard);
        optionsGroup.add(advance);
        
        option.add(standard);
        option.add(advance);
        
        return option;
    }
    
    private class CalculatorOptionAction implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() instanceof JRadioButton) {
                JRadioButton button = (JRadioButton) e.getSource();
                
                if (button.getText().equals("Estandar")) {
                    System.out.println("Standard action");
                    panelCalculadora.remove(panelAvanzado);
                    
                    
                } else if (button.getText().equals("Avanzado")) {
                    System.out.println("Advance action");
                    panelCalculadora.add(panelAvanzado);
                }
                
                panelCalculadora.updateUI();
                ventana.pack();
            }
        }
        
    }
}
